package com.flyduck.vueshop.system.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.vueshop.common.core.lang.Result;
import com.flyduck.vueshop.common.mybatis.base.BaseController;
import com.flyduck.vueshop.system.admin.service.AdminUserService;
import com.flyduck.vueshop.system.admin.vo.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * SysUserController
 * </p>
 *
 * @author flyduck
 * @since 2024-06-03
 */
@RestController
@RequestMapping("/system/admin/user")
public class AdminUserController extends BaseController {

    @Resource
    private AdminUserService adminUserService;

    @GetMapping("/getUserPage")
    @SaCheckPermission("sys:user:list")
    public Result<Page<AdminUserRespVO>> getUserPage(AdminUserQueryReqVO adminUserQueryReqVO){
        Page<AdminUserRespVO> sysUserRespVOPage = adminUserService.getUserPage(getPage(), adminUserQueryReqVO);
        return Result.success(sysUserRespVOPage);
    }

    @GetMapping("/getUserDetailsById/{id}")
    @SaCheckPermission("sys:user:list")
    public Result<AdminUserDetailsRespVO> getUserDetailsById(@PathVariable("id") Long id){
        AdminUserDetailsRespVO adminUserDetailsRespVO = adminUserService.getUserDetailsById(id);
        return Result.success(adminUserDetailsRespVO);
    }

    @PostMapping("/saveUser")
    @SaCheckPermission("sys:user:save")
    public Result<Void> saveUser(@Validated @RequestBody AdminUserSaveReqVO adminUserSaveReqVO){
        adminUserService.saveUser(adminUserSaveReqVO);
        return Result.success();
    }

    @PostMapping("/updateUser")
    @SaCheckPermission("sys:user:update")
    public Result<Void> updateUser(@Validated @RequestBody AdminUserUpdateReqVO adminUserUpdateReqVO){
        adminUserService.updateUser(adminUserUpdateReqVO);
        return Result.success();
    }

    @PostMapping("/batchDeleteByIds")
    @SaCheckPermission("sys:user:delete")
    public Result<Void> batchDeleteByIds(@RequestBody Long[] ids){
        adminUserService.batchDeleteByIds(Arrays.asList(ids));
        return Result.success();
    }

    @PostMapping("/assignRolesToUser/{userId}")
    @SaCheckPermission("sys:user:perm")
    public Result<Void> assignRolesToUser(@PathVariable(name = "userId")Long userId, @RequestBody Long[] roleIds){
        adminUserService.assignRolesToUser(userId,Arrays.asList(roleIds));
        return Result.success();
    }

    @PostMapping("/resetPasswordById/{id}")
    @SaCheckPermission("sys:user:resetPwd")
    public Result<Void> resetPasswordById(@PathVariable("id") Long id){
        adminUserService.resetPasswordById(id);
        return Result.success();
    }

}
